<?php

namespace app\ask\controller;

use think\Controller;

class Collect extends Controller
{
    protected $CollectModel;
    protected $CateModel;

    public function __construct()
    {
        parent::__construct();

        $this->CollectModel = model('post.Collect');
        $this->CateModel = model('post.Cate');
    }

    public function index()
    {
        $page = $this->request->param('page', 1, 'intval');
        $limit = $this->request->param('limit', 10, 'intval');
        $busid = $this->request->param('busid', 0, 'trim');

        $offset = ($page - 1) * $limit;

        $where = ['fa_post_collect.busid' => $busid];

        // if ($busid) {
        //     $where['busid'] = $busid;
        // }

        $total = $this->CollectModel
            ->where($where)
            ->count();

        $list = $this->CollectModel
            ->with(['business', 'post'])
            ->where($where)
            ->limit($offset, $limit)
            ->select();


        if (empty($list)) {
            $this->success('暂无收藏数据', null, ['list' => [], 'total' => 0]);
            exit;
        }

        foreach ($list as &$item) {
            $item = $item->toArray();

            $item['business'] = filter_property($item['business'], ['id', 'avatar_text', 'nickname']);

            $item['post']['business'] = $item['business'];

            $item['post']['cate'] = $this->CateModel->find($item['post']['cateid']);
        }

        $this->success('返回收藏列表', null, ['list' => $list, 'total' => $total]);
        exit;
    }
}
